﻿namespace KeRui.DAL
{
    using KeRui.Common;
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsContent
    {
        public int Add(KeRui.Model.CmsContent model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@ContentID", SqlDbType.Int, 4), new SqlParameter("@NodeID", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@TitleStyle", SqlDbType.NVarChar), new SqlParameter("@Summary", SqlDbType.NVarChar, 400), new SqlParameter("@IsRecommend", SqlDbType.Bit, 1), new SqlParameter("@IsHot", SqlDbType.Bit, 1), new SqlParameter("@IsMarked", SqlDbType.Bit, 1), new SqlParameter("@IsTop", SqlDbType.Bit, 1), new SqlParameter("@ContentState", SqlDbType.Int, 4), new SqlParameter("@PublicState", SqlDbType.Int, 4), new SqlParameter("@PublicStartTime", SqlDbType.DateTime), new SqlParameter("@PublicEndTime", SqlDbType.DateTime), new SqlParameter("@TemplateFile", SqlDbType.VarChar, 250), new SqlParameter("@FilePath", SqlDbType.VarChar, 500), new SqlParameter("@FileDir", SqlDbType.VarChar, 900), 
                new SqlParameter("@EnableComment", SqlDbType.Bit, 1), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), new SqlParameter("@Guid", SqlDbType.VarChar, 0x24), new SqlParameter("@PublishTime", SqlDbType.DateTime), new SqlParameter("@PicUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@ContentType", SqlDbType.Int, 4), new SqlParameter("@SellerID", SqlDbType.Int, 4)
             };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.NodeID;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.TitleStyle;
            parameters[4].Value = model.Summary;
            parameters[5].Value = model.IsRecommend;
            parameters[6].Value = model.IsHot;
            parameters[7].Value = model.IsMarked;
            parameters[8].Value = model.IsTop;
            parameters[9].Value = model.ContentState;
            parameters[10].Value = model.PublicState;
            parameters[11].Value = model.PublicStartTime;
            parameters[12].Value = model.PublicEndTime;
            parameters[13].Value = model.TemplateFile;
            parameters[14].Value = model.FilePath;
            parameters[15].Value = model.FileDir;
            parameters[0x10].Value = model.EnableComment;
            parameters[0x11].Value = model.UserID;
            parameters[0x12].Value = model.CreateIP;
            parameters[0x13].Value = Guid.NewGuid().ToString();
            parameters[20].Value = model.PublishTime;
            parameters[0x15].Value = model.PicUrl;
            parameters[0x16].Value = model.ContentType;
            parameters[0x17].Value = 0;
            DbHelperSQL.RunProcedure("CmsContent_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void AddHits(int ContentID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int, 4) };
            parameters[0].Value = ContentID;
            DbHelperSQL.RunProcedure("CmsContent_AdHits", parameters, out num);
        }

        public void AppendContent(int contentID, string content)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int), new SqlParameter("@Content", SqlDbType.NText) };
            parameters[0].Value = contentID;
            parameters[1].Value = content;
            DbHelperSQL.RunProcedure("CmsContent_AppendContent", parameters);
        }

        public void Check(int contentID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int) };
            parameters[0].Value = contentID;
            DbHelperSQL.RunProcedure("CmsContent_Check", parameters);
        }

        public void Delete(int contentID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int) };
            parameters[0].Value = contentID;
            DbHelperSQL.RunProcedure("CmsContent_Delete", parameters);
        }

        public void DeleteByNodeID(int nodeID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int) };
            parameters[0].Value = nodeID;
            DbHelperSQL.RunProcedure("CmsContent_DeleteByNodeID", parameters);
        }

        public int FixPreNextID(int contentID, string action)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int), new SqlParameter("@Action", SqlDbType.VarChar, 20) };
            parameters[0].Value = contentID;
            parameters[1].Value = action;
            return DbHelperSQL.RunProcedure("CmsContent_FixPreNextID", parameters, out num);
        }

        public DataTable GetCmsContentDetailsByNodeID(int nodeID, string top, string strWhere, string indexRange, string orderby)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int), new SqlParameter("@Top", SqlDbType.VarChar, 100), new SqlParameter("@strWhere", SqlDbType.VarChar, 0x7d0), new SqlParameter("@IndexRange", SqlDbType.VarChar, 500), new SqlParameter("@OrderBy", SqlDbType.VarChar, 100) };
            parameters[0].Value = nodeID;
            parameters[1].Value = top;
            parameters[2].Value = strWhere;
            parameters[3].Value = indexRange;
            parameters[4].Value = orderby;
            DataSet set = DbHelperSQL.RunProcedure("CmsContent_GetDetailsByNodeID", parameters, "ds");
            if (set.Tables.Count > 0)
            {
                return set.Tables[0];
            }
            return null;
        }

        public DataTable GetCmsContentDetailsByParentNodeID(int parentNodeID, string top, string strWhere, string indexRange, string orderby)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ParentNodeID", SqlDbType.Int), new SqlParameter("@Top", SqlDbType.VarChar, 100), new SqlParameter("@strWhere", SqlDbType.VarChar, 0x7d0), new SqlParameter("@IndexRange", SqlDbType.VarChar, 500), new SqlParameter("@OrderBy", SqlDbType.VarChar, 100) };
            parameters[0].Value = parentNodeID;
            parameters[1].Value = top;
            parameters[2].Value = strWhere;
            parameters[3].Value = indexRange;
            parameters[4].Value = orderby;
            DataSet set = DbHelperSQL.RunProcedure("CmsContent_GetDetailsByParentNodeID", parameters, "ds");
            if (set.Tables.Count <= 0)
            {
                return null;
            }
            return set.Tables[0];
        }

        public DataTable GetCmsContentRelativeNews(int nodeID, string type, int top, string dateRange)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int), new SqlParameter("@Type", SqlDbType.VarChar, 20), new SqlParameter("@Top", SqlDbType.Int), new SqlParameter("@DateRange", SqlDbType.VarChar, 200) };
            parameters[0].Value = nodeID;
            parameters[1].Value = type;
            parameters[2].Value = top;
            parameters[3].Value = dateRange;
            return DbHelperSQL.RunProcedure("CmsContent_GetRelativeNews", parameters, "ds").Tables[0];
        }

        public DataTable GetContentIDByNodeID(int NodeID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int) };
            parameters[0].Value = NodeID;
            return DbHelperSQL.RunProcedure("CmsContent_GetContentIDByNodeID", parameters, "ds").Tables[0];
        }

        public int GetCountByNodeID(int nodeID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int) };
            parameters[0].Value = nodeID;
            return DbHelperSQL.RunProcedure("CmsContent_GetCountByNodeID", parameters, out num);
        }

        public int GetCountByParentNodeID(int nodeID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ParentNodeID", SqlDbType.Int) };
            parameters[0].Value = nodeID;
            return (int) DbHelperSQL.RunProcedure("CmsContent_GetCountByParentNodeID", parameters, "ds").Tables[0].Rows[0][0];
        }

        public KeRui.Model.CmsContent GetModel(int ContentID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int, 4) };
            parameters[0].Value = ContentID;
            KeRui.Model.CmsContent content = new KeRui.Model.CmsContent();
            DataSet set = DbHelperSQL.RunProcedure("CmsContent_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ContentID"].ToString() != "")
                {
                    content.ContentID = int.Parse(set.Tables[0].Rows[0]["ContentID"].ToString());
                }
                if (set.Tables[0].Rows[0]["NodeID"].ToString() != "")
                {
                    content.NodeID = int.Parse(set.Tables[0].Rows[0]["NodeID"].ToString());
                }
                content.NodeName = set.Tables[0].Rows[0]["NodeName"].ToString();
                if (set.Tables[0].Rows[0]["ModelID"].ToString() != "")
                {
                    content.ModelID = int.Parse(set.Tables[0].Rows[0]["ModelID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentType"].ToString() != "")
                {
                    content.ContentType = int.Parse(set.Tables[0].Rows[0]["ContentType"].ToString());
                }
                content.Title = set.Tables[0].Rows[0]["Title"].ToString();
                content.TitleStyle = set.Tables[0].Rows[0]["TitleStyle"].ToString();
                content.Summary = set.Tables[0].Rows[0]["Summary"].ToString();
                if (set.Tables[0].Rows[0]["IsRecommend"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsRecommend"].ToString() == "1") || (set.Tables[0].Rows[0]["IsRecommend"].ToString().ToLower() == "true"))
                    {
                        content.IsRecommend = true;
                    }
                    else
                    {
                        content.IsRecommend = false;
                    }
                }
                if (set.Tables[0].Rows[0]["IsHot"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsHot"].ToString() == "1") || (set.Tables[0].Rows[0]["IsHot"].ToString().ToLower() == "true"))
                    {
                        content.IsHot = true;
                    }
                    else
                    {
                        content.IsHot = false;
                    }
                }
                if (set.Tables[0].Rows[0]["IsMarked"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsMarked"].ToString() == "1") || (set.Tables[0].Rows[0]["IsMarked"].ToString().ToLower() == "true"))
                    {
                        content.IsMarked = true;
                    }
                    else
                    {
                        content.IsMarked = false;
                    }
                }
                if (set.Tables[0].Rows[0]["IsTop"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsTop"].ToString() == "1") || (set.Tables[0].Rows[0]["IsTop"].ToString().ToLower() == "true"))
                    {
                        content.IsTop = true;
                    }
                    else
                    {
                        content.IsTop = false;
                    }
                }
                if (set.Tables[0].Rows[0]["ContentState"].ToString() != "")
                {
                    content.ContentState = int.Parse(set.Tables[0].Rows[0]["ContentState"].ToString());
                }
                if (set.Tables[0].Rows[0]["PublicState"].ToString() != "")
                {
                    content.PublicState = int.Parse(set.Tables[0].Rows[0]["PublicState"].ToString());
                }
                if (set.Tables[0].Rows[0]["PublicStartTime"].ToString() != "")
                {
                    content.PublicStartTime = DateTime.Parse(set.Tables[0].Rows[0]["PublicStartTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["PublicEndTime"].ToString() != "")
                {
                    content.PublicEndTime = DateTime.Parse(set.Tables[0].Rows[0]["PublicEndTime"].ToString());
                }
                content.TemplateFile = set.Tables[0].Rows[0]["TemplateFile"].ToString();
                content.FilePath = set.Tables[0].Rows[0]["FilePath"].ToString();
                content.FileDir = set.Tables[0].Rows[0]["FileDir"].ToString();
                if (set.Tables[0].Rows[0]["FileUpdateTime"].ToString() != "")
                {
                    content.FileUpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["FileUpdateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["Hits"].ToString() != "")
                {
                    content.Hits = int.Parse(set.Tables[0].Rows[0]["Hits"].ToString());
                }
                if (set.Tables[0].Rows[0]["EnableComment"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["EnableComment"].ToString() == "1") || (set.Tables[0].Rows[0]["EnableComment"].ToString().ToLower() == "true"))
                    {
                        content.EnableComment = true;
                    }
                    else
                    {
                        content.EnableComment = false;
                    }
                }
                if (set.Tables[0].Rows[0]["CommentCount"].ToString() != "")
                {
                    content.CommentCount = int.Parse(set.Tables[0].Rows[0]["CommentCount"].ToString());
                }
                if (set.Tables[0].Rows[0]["PrevID"].ToString() != "")
                {
                    content.PrevID = int.Parse(set.Tables[0].Rows[0]["PrevID"].ToString());
                }
                if (set.Tables[0].Rows[0]["NextID"].ToString() != "")
                {
                    content.NextID = int.Parse(set.Tables[0].Rows[0]["NextID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentPages"].ToString() != "")
                {
                    content.ContentPages = int.Parse(set.Tables[0].Rows[0]["ContentPages"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    content.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                content.Username = set.Tables[0].Rows[0]["Username"].ToString();
                if (set.Tables[0].Rows[0]["SellerID"].ToString() != "")
                {
                    content.SellerID = int.Parse(set.Tables[0].Rows[0]["SellerID"].ToString());
                }
                content.SellerName = set.Tables[0].Rows[0]["SellerName"].ToString();
                if (set.Tables[0].Rows[0]["IsDelete"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsDelete"].ToString() == "1") || (set.Tables[0].Rows[0]["IsDelete"].ToString().ToLower() == "true"))
                    {
                        content.IsDelete = true;
                    }
                    else
                    {
                        content.IsDelete = false;
                    }
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    content.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                content.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    content.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                content.Guid = set.Tables[0].Rows[0]["Guid"].ToString();
                if (set.Tables[0].Rows[0]["AttID"].ToString() != "")
                {
                    content.AttID = int.Parse(set.Tables[0].Rows[0]["AttID"].ToString());
                }
                if (set.Tables[0].Rows[0]["PublishTime"].ToString() != "")
                {
                    content.PublishTime = DateTime.Parse(set.Tables[0].Rows[0]["PublishTime"].ToString());
                }
                return content;
            }
            return null;
        }

        public DataSet GetModelByContentID(int contentID, string strWhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int), new SqlParameter("@Where", SqlDbType.VarChar, -1) };
            parameters[0].Value = contentID;
            parameters[1].Value = strWhere;
            return DbHelperSQL.RunProcedure("CmsContent_GetModelByContentID", parameters, "ds");
        }

        public DataSet GetModelDetailsByContentID(int contentID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int) };
            parameters[0].Value = contentID;
            return DbHelperSQL.RunProcedure("CmsContent_GetModelDetailsByContentID", parameters, "ds");
        }

        public void MoveContentByContentID(string ContentIDs, int oldNode, int newNode, int num)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentIDs", SqlDbType.NVarChar), new SqlParameter("@oldNodeID", SqlDbType.Int), new SqlParameter("@newNodeID", SqlDbType.Int), new SqlParameter("@count", SqlDbType.Int) };
            parameters[0].Value = ContentIDs;
            parameters[1].Value = oldNode;
            parameters[2].Value = newNode;
            parameters[3].Value = num;
            DbHelperSQL.RunProcedure("CmsContent_MoveContentByContentID", parameters);
        }

        public void MoveContentByNodeID(int oldNode, int newNode)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@oldNodeID", SqlDbType.Int), new SqlParameter("@newNodeID", SqlDbType.Int) };
            parameters[0].Value = oldNode;
            parameters[1].Value = newNode;
            DbHelperSQL.RunProcedure("CmsContent_MoveContentByNodeID", parameters);
        }

        public DataTable Search(string keyword)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Keyword", SqlDbType.NVarChar, 200) };
            parameters[0].Value = keyword;
            return DbHelperSQL.RunProcedure("CmsContent_Search", parameters, "ds").Tables[0];
        }

        public DataSet SearchList(string strWhere, int PageSize, int PageIndex)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@strWhere", SqlDbType.NText), new SqlParameter("@PageSize", SqlDbType.Int), new SqlParameter("@PageIndex", SqlDbType.Int) };
            parameters[0].Value = strWhere;
            parameters[1].Value = PageSize;
            parameters[2].Value = PageIndex;
            return DbHelperSQL.RunProcedure("CmsContent_SearchList", parameters, "ds");
        }

        public void Update(KeRui.Model.CmsContent model)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@ContentID", SqlDbType.Int, 4), new SqlParameter("@NodeID", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@TitleStyle", SqlDbType.NVarChar), new SqlParameter("@Summary", SqlDbType.NVarChar, 400), new SqlParameter("@IsRecommend", SqlDbType.Bit, 1), new SqlParameter("@IsHot", SqlDbType.Bit, 1), new SqlParameter("@IsMarked", SqlDbType.Bit, 1), new SqlParameter("@IsTop", SqlDbType.Bit, 1), new SqlParameter("@ContentState", SqlDbType.Int, 4), new SqlParameter("@PublicState", SqlDbType.Int, 4), new SqlParameter("@PublicStartTime", SqlDbType.DateTime), new SqlParameter("@PublicEndTime", SqlDbType.DateTime), new SqlParameter("@TemplateFile", SqlDbType.VarChar, 250), new SqlParameter("@FilePath", SqlDbType.VarChar, 500), new SqlParameter("@FileDir", SqlDbType.VarChar, 900), 
                new SqlParameter("@EnableComment", SqlDbType.Bit, 1), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), new SqlParameter("@Guid", SqlDbType.VarChar, 0x24), new SqlParameter("@PublishTime", SqlDbType.DateTime), new SqlParameter("@PicUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@ContentType", SqlDbType.Int, 4)
             };
            parameters[0].Value = model.ContentID;
            parameters[1].Value = model.NodeID;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.TitleStyle;
            parameters[4].Value = model.Summary;
            parameters[5].Value = model.IsRecommend;
            parameters[6].Value = model.IsHot;
            parameters[7].Value = model.IsMarked;
            parameters[8].Value = model.IsTop;
            parameters[9].Value = model.ContentState;
            parameters[10].Value = model.PublicState;
            parameters[11].Value = model.PublicStartTime;
            parameters[12].Value = model.PublicEndTime;
            parameters[13].Value = model.TemplateFile;
            parameters[14].Value = model.FilePath;
            parameters[15].Value = model.FileDir;
            parameters[0x10].Value = model.EnableComment;
            parameters[0x11].Value = model.UserID;
            parameters[0x12].Value = Utils.GetRealIP();
            parameters[0x13].Value = Guid.NewGuid().ToString();
            parameters[20].Value = model.PublishTime;
            parameters[0x15].Value = model.PicUrl;
            parameters[0x16].Value = model.ContentType;
            DbHelperSQL.RunProcedure("CmsContent_Update", parameters);
        }

        public int UpdateFourStatus(int contentID, string type)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int), new SqlParameter("@Type", SqlDbType.NText) };
            parameters[0].Value = contentID;
            parameters[1].Value = type;
            return DbHelperSQL.RunProcedure("CmsContent_UpdateFourStatus", parameters, out num);
        }
    }
}

